package com.edgar.desktopwidget;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.RemoteViews;
import android.widget.TextView;

import com.edgar.main.R;

public class DesktopNoteWidgetActivity extends Activity {

		int mAppWidgetId;
		TextView mTextView;
		ImageButton mImBtn1, mImBtn2, mImBtn3, mImBtn4;

		final String mPerfName = "DesktopNote";
		SharedPreferences mPref;

		@Override
		protected void onCreate(Bundle savedInstanceState) {
			// TODO Auto-generated method stub
			super.onCreate(savedInstanceState);
			setContentView(R.layout.desktop_main);

			Intent t = getIntent();
			Log.i("myLog", t.getAction());
			mAppWidgetId = t.getExtras().getInt(
					AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
			Log.i("myLog", "it's [" + mAppWidgetId + "] editing!");

			mPref = getSharedPreferences(mPerfName, 0);
			String noteContent = mPref.getString("DAT" + mAppWidgetId, "");

			mTextView = (TextView) findViewById(R.id.EditText02);
			mTextView.setText(noteContent);
			mImBtn1 = (ImageButton) findViewById(R.id.desktop_main_img1);
			mImBtn2 = (ImageButton) findViewById(R.id.desktop_main_img2);
			mImBtn3 = (ImageButton) findViewById(R.id.desktop_main_img3);
			mImBtn4 = (ImageButton) findViewById(R.id.desktop_main_img4);

			mImBtn1.setOnClickListener(mBtnClick);
			mImBtn2.setOnClickListener(mBtnClick);
			mImBtn3.setOnClickListener(mBtnClick);
			mImBtn4.setOnClickListener(mBtnClick);

		}

		OnClickListener mBtnClick = new OnClickListener() {

			@Override
			public void onClick(View v) {

				SharedPreferences.Editor prefsEdit = mPref.edit();
				prefsEdit.putString("DAT" + mAppWidgetId, mTextView.getText()
						.toString());
				prefsEdit.commit();

				int srcId = R.drawable.desktop_img0;
				switch (v.getId()) {
				case R.id.desktop_main_img1:
					srcId = R.drawable.desktop_img1;
					break;
				case R.id.desktop_main_img2:
					srcId = R.drawable.desktop_img2;
					break;
				case R.id.desktop_main_img3:
					srcId = R.drawable.desktop_img3;
					break;
				case R.id.desktop_main_img4:
					srcId = R.drawable.desktop_img4;
					break;
				}

				RemoteViews views = new RemoteViews(DesktopNoteWidgetActivity.this
						.getPackageName(), R.layout.desktop_widget);
				views.setImageViewResource(R.id.desktop_img, srcId);

				AppWidgetManager appWidgetManager = AppWidgetManager
						.getInstance(DesktopNoteWidgetActivity.this);
				appWidgetManager.updateAppWidget(mAppWidgetId, views);

				DesktopNoteWidgetActivity.this.finish();
			}
		};
	}